﻿function NumberOnly(e) {

    if ((e.keyCode >= 48 && e.keyCode <= 57) || (e.keyCode == 8)) {
        return true;
    }
    else {
        return false;
    }
}

function CloseForm() {
    window.close();
}

//function popWindow(strURL, intWidth, intHeight) {
//    var sFileName;
//    sFileName = strURL;
//    window.open(sFileName, "aa", "scrollbars=yes,menubar=no,height=" + intHeight + ",width=" + intWidth + ",resizable=no,toolbar=no,location=no,status=no");
//    return false;
//}

function popWindow(pageURL, w, h) {
    var left = (screen.width / 2) - (w / 2);
    var top = (screen.height / 2) - (h / 2);
    var targetWin = window.open(pageURL, 'aa', 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=no, width=' + w + ', height=' + h + ', top=' + top + ', left=' + left);
    return false;
}

var CheckPopup = "";
//Lost focus textbox call ajax for file xml
function JFocusLostMultiCenter(xmlKey, listControl, controlRefresh, boolRefreshNullValue, boolRefreshClearData, queryString, queryStringClientID, searchText, urlMethod, Method) {
    $("#" + searchText).focusout(function () {

        if (CheckPopup == "1") {
            CheckPopup = "";
            return;
        }

        if (document.getElementById(listControl) != null) {
            var getArray = document.getElementById(listControl).value;
            var listArray = getArray.split('~');
        }
        else {
            var listArray = listControl.split('~');
        }

        var NextControl = $(this).next('input');
        var sSearch = $("#" + searchText).val().replace(/\\/g, '');

        if (jQuery.trim(sSearch).length > 0) {

            if (document.getElementById(searchText).defaultValue != "") {
                if (document.getElementById(searchText).old_value === undefined) {
                    if (document.getElementById(searchText).defaultValue != $("#" + searchText).val()) {
                        document.getElementById(searchText).old_value = document.getElementById(searchText).defaultValue;
                    }
                    else {
                        document.getElementById(searchText).old_value = $("#" + searchText).val();
                    }
                }
            }
            else {
                if (document.getElementById(searchText).old_value === undefined) {
                    document.getElementById(searchText).old_value = "";
                }
            }

            sOldSearch = document.getElementById(searchText).old_value;

            var sQry = "";

            if (queryString != "") {
                sQry += queryString;
            }

            if (queryStringClientID != "") {

                var listClientID = queryStringClientID.split('&');
                var intIndex = 0;
                var sName = "";
                var sValue = "";

                for (var i = 0; i < listClientID.length; i++) {
                    if (listClientID[i] != "") {
                        intIndex = listClientID[i].indexOf("=")
                        if (intIndex != -1) {
                            sName = listClientID[i].substring(0, intIndex + 1);
                            sValue = listClientID[i].substring(intIndex + 1, listClientID[i].length);
                            if (sValue != "") {
                                if (document.getElementById(sValue) != null) {
                                    if (document.getElementById(sValue).value != "")
                                        sQry += "&" + sName + document.getElementById(sValue).value;
                                    else
                                        sQry += "&" + sName + "";
                                }
                            }
                        }
                    }
                }
            }

            if (sOldSearch != sSearch) {
                $.ajax({
                    type: "POST",
                    url: urlMethod + "/" + Method,
                    data: '{"strSearchKey":"' + xmlKey + '","strQuerystring":"' + sQry + '","strSearch":"' + sSearch + '"}',
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (txt) {
                        var intRegex = /^\d+$/;

                        if (txt.d == "NoFind") {

                            if (document.getElementById(listControl) != null) {
                                document.getElementById(listControl).value = "";
                            }
                            else {
                                for (var i = 0; i < listArray.length; i++) {
                                    if (document.getElementById(listArray[i]) != null) {
                                        document.getElementById(listArray[i]).value = "";
                                    }
                                }
                            }

                            document.getElementById(searchText).old_value = "";
                            alert('ไม่พบข้อมูลที่คุณค้นหา');
                        }
                        else {
                            var substr = txt.d.split('$');

                            if (document.getElementById(listControl) != null) {
                                document.getElementById(listControl).value = substr[0];
                            }
                            else {
                                for (var i = 0; i < listArray.length; i++) {
                                    if (document.getElementById(listArray[i]) != null) {
                                        document.getElementById(listArray[i]).value = substr[i];
                                    }
                                }
                            }

                            if (controlRefresh != "") {
                                if (sOldSearch != sSearch) {
                                    document.getElementById(searchText).old_value = $("#" + searchText).val();
                                    document.getElementById(controlRefresh).click();
                                }
                            }

                            document.getElementById(searchText).old_value = $("#" + searchText).val();
                            NextControl.focus();

                        }
                    },
                    error: function (xhr, status, error) {
                        alert(xhr.responseText);
                    }
                });
            }
            else {
                document.getElementById(searchText).old_value = $("#" + searchText).val();
                NextControl.focus();
            }
        }
        else {

            if (document.getElementById(listControl) != null) {
                document.getElementById(listControl).value = "";
            }
            else {
                for (var i = 0; i < listArray.length; i++) {
                    if (document.getElementById(listArray[i]) != null) {
                        document.getElementById(listArray[i]).value = "";
                    }
                }
            }

            document.getElementById(searchText).old_value = "";

            if (boolRefreshClearData == "True") {
                if (controlRefresh != "") {
                    if (document.getElementById(searchText).defaultValue != "") {
                        document.getElementById(controlRefresh).click();
                    }
                }
            }
        }
    });
}

//Enter textbox call ajax
function JKeyEnterMultiCenter(xmlKey, listControl, controlRefresh, boolRefreshNullValue, queryString, queryStringClientID, searchWithField, searchText, urlMethod, Method) {
    $("#" + searchText).bind('keydown', function (e) {
        var sCheck = searchText;
        if (e.target.id == sCheck) {
            if (e.keyCode == 13) {
                CheckPopup = "1";
                if (document.getElementById(listControl) != null) {
                    var getArray = document.getElementById(listControl).value;
                    var listArray = getArray.split('~');
                }
                else {
                    var listArray = listControl.split('~');
                }

                var NextControl = $(this).next('input');
                var sSearch = $("#" + searchText).val().replace(/\\/g, '');

                if (jQuery.trim(sSearch).length > 0) {

                    if (document.getElementById(searchText).defaultValue != "") {
                        if (document.getElementById(searchText).old_value === undefined) {
                            if (document.getElementById(searchText).defaultValue != $("#" + searchText).val()) {
                                document.getElementById(searchText).old_value = document.getElementById(searchText).defaultValue;
                            }
                            else {
                                document.getElementById(searchText).old_value = $("#" + searchText).val();
                            }
                        }
                    }
                    else {
                        if (document.getElementById(searchText).old_value === undefined) {
                            document.getElementById(searchText).old_value = "";
                        }
                    }

                    sOldSearch = document.getElementById(searchText).old_value;

                    var sQry = "";

                    if (queryString != "") {
                        sQry += queryString;
                    }

                    if (queryStringClientID != "") {

                        var listClientID = queryStringClientID.split('&');
                        var intIndex = 0;
                        var sName = "";
                        var sValue = "";

                        for (var i = 0; i < listClientID.length; i++) {
                            if (listClientID[i] != "") {
                                intIndex = listClientID[i].indexOf("=")
                                if (intIndex != -1) {
                                    sName = listClientID[i].substring(0, intIndex + 1);
                                    sValue = listClientID[i].substring(intIndex + 1, listClientID[i].length);
                                    if (sValue != "") {
                                        if (document.getElementById(sValue) != null) {
                                            if (document.getElementById(sValue).value != "")
                                                sQry += "&" + sName + document.getElementById(sValue).value;
                                            else
                                                sQry += "&" + sName + "";
                                        }
                                    }
                                }
                            }
                        }
                    }

                    if (sOldSearch != sSearch) {
                        $.ajax({
                            type: "POST",
                            url: urlMethod + "/" + Method,
                            data: '{"strSearchKey":"' + xmlKey + '","strQuerystring":"' + sQry + '","StrSearchWithField":"' + searchWithField + '","strSearch":"' + sSearch + '"}',
                            contentType: "application/json; charset=utf-8",
                            dataType: "json",
                            success: function (txt) {
                                var intRegex = /^\d+$/;

                                if (txt.d == "NoFind") {

                                    if (document.getElementById(listControl) != null) {
                                        document.getElementById(listControl).value = "";
                                    }
                                    else {
                                        for (var i = 0; i < listArray.length; i++) {
                                            if (document.getElementById(listArray[i]) != null) {
                                                document.getElementById(listArray[i]).value = "";
                                            }
                                        }
                                    }

                                    document.getElementById(searchText).old_value = "";
                                    alert('ไม่พบข้อมูลที่คุณค้นหา');
                                }
                                else {
                                    var substr = txt.d.split('$');

                                    if (document.getElementById(listControl) != null) {
                                        document.getElementById(listControl).value = substr[0];
                                    }
                                    else {
                                        for (var i = 0; i < listArray.length; i++) {
                                            if (document.getElementById(listArray[i]) != null) {
                                                document.getElementById(listArray[i]).value = substr[i];
                                            }
                                        }
                                    }

                                    if (controlRefresh != "") {
                                        if (sOldSearch != sSearch) {
                                            document.getElementById(searchText).old_value = $("#" + searchText).val();
                                            document.getElementById(controlRefresh).click();
                                        }
                                    }

                                    document.getElementById(searchText).old_value = $("#" + searchText).val();
                                    NextControl.focus();

                                }
                            },
                            error: function (xhr, status, error) {
                                alert(xhr.responseText);
                            }
                        });
                    }
                    else {
                        document.getElementById(searchText).old_value = $("#" + searchText).val();
                        NextControl.focus();
                    }
                }
                else {
                    if (document.getElementById(listControl) != null) {
                        document.getElementById(listControl).value = "";
                    }
                    else {
                        for (var i = 0; i < listArray.length; i++) {
                            if (document.getElementById(listArray[i]) != null) {
                                document.getElementById(listArray[i]).value = "";
                            }
                        }
                    }

                    document.getElementById(searchText).old_value = "";

                }
                e.preventDefault();
                return false;
            }
        }
        else
            if (e.keyCode == 13) {
                e.preventDefault();
                return false;
            }
            else
                return true;
    });
}